package swingUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import mySwingToolkit.model.ClassModel;
import mySwingToolkit.ui.ButtonTabComponent;

import com.trsjh.db.controller.Home;
import com.trsjh.db.domains.Customer;
import com.trsjh.db.domains.CustomerAccount;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainUI extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3504527185281049744L;
	private JTextField nameText;
	private JTextField phoneText;
	private CustomerAccountViewUI tv;

	/**
	 * Create the panel.
	 */
	public MainUI() {

		JLabel label = new JLabel("手机号");

		phoneText = new JTextField();
		phoneText.setColumns(10);

		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				search();
			}
		});

		JLabel label_1 = new JLabel("姓名");

		nameText = new JTextField();
		nameText.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(141)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(163)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 67, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(label, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
							.addGap(49)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(nameText, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
								.addComponent(phoneText, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))))
					.addGap(142))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(94)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(2)
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))
						.addComponent(phoneText, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
					.addGap(29)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(nameText, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
					.addGap(72)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
					.addGap(94))
		);
		setLayout(groupLayout);
	}

	@SuppressWarnings("unchecked")
	protected void search() {
		String phone=phoneText.getText().trim();
		String name=nameText.getText().trim();

		Home home=new Home();
		try{
			Map<Integer,Object> params=new HashMap<Integer,Object>();
			List<CustomerAccount> customers=null;
			if(phone.equals("")){
				if(name.equals("")){
					JOptionPane.showMessageDialog(this, "手机号和姓名至少必须填写一个。");
				}else{
					params.put(1, name);
					customers=home.list(Home.GET_CUSTOMERS_BY_NAME,params);
				}
			}else{
				if(name.equals("")){	
					params.put(1, phone);
					Customer c=(Customer)home.getFirst(Home.GET_CUSTOMERS_BY_PHONE,params);

					if(c!=null){
						System.out.println(c.getPhone()+""+c.getId());
						customers=new ArrayList<CustomerAccount>(c.getCustomerAccounts());
					}
				}else{
					params.put(1, phone);
					params.put(2, name);
					customers=home.list(Home.GET_CUSTOMER_BY_PHONE_AND_NAME,params);
					System.out.println(customers.size());
				}
			}
			int result=0;
			
			JTabbedPane tabbedPane=((JTabbedPane)this.getParent());
			
			if(customers==null||customers.size()==0){
				result=JOptionPane.showConfirmDialog(this, "没有该用户的记录，是否添加新用户", "查询反馈", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
				if(result==JOptionPane.YES_OPTION){
					CustomerAccountUI newCa=new CustomerAccountUI(phone,name);
					tabbedPane.add(App.TAB_ADD_CUSTOMER,newCa);
					tabbedPane.setSelectedComponent(newCa);
					tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(newCa), new ButtonTabComponent(tabbedPane));
				}
			}
			else{
				tv=new CustomerAccountViewUI(customers,20);
				tabbedPane.add(App.TAB_CUSTOMER2,tv);
				tabbedPane.setSelectedComponent(tv);
				tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(tv), new ButtonTabComponent(tabbedPane));
				
				JTable jt=tv.getTable();
				jt.addMouseListener(new MouseAdapter(){
					@Override
					public void mouseClicked(MouseEvent e) {
						selectUser();
					}
				});
			}
		}finally{
			home.closeEntityManager();
		}
	}
	private void selectUser(){
		if(tv.getTable().getSelectedColumn()==9){
			@SuppressWarnings("unchecked")
			ClassModel<CustomerAccount> cm=(ClassModel<CustomerAccount>) tv.getTable().getValueAt(tv.getTable().getSelectedRow(),0);
			CustomerAccount ca=cm.getObject();
			JTabbedPane tabbedPane=((JTabbedPane)this.getParent());
			CustomerRecordViewUI crui=new CustomerRecordViewUI(ca);
			tabbedPane.setSelectedComponent(
					tabbedPane.add(ca.getName()+App.TAB_CUSTOMERRECORDS,crui)
					);
			tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(crui), new ButtonTabComponent(tabbedPane));
		}
	}
	
	 
	
	
}
